package com.bwie.service.impl;

import com.bwie.exception.BusinessException;
import com.bwie.mapper.ApplyMapper;
import com.bwie.mapper.OrderParkMapper;
import com.bwie.pojo.dto.ApplyDto;
import com.bwie.pojo.po.Apply;
import com.bwie.pojo.po.OrderPark;
import com.bwie.pojo.vo.UserVo;
import com.bwie.service.ApplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private OrderParkMapper orderParkMapper;
    @Override
    public void addApply(Apply apply) {

        UserVo user = (UserVo) session.getAttribute("user");
        if (user.getUserId()==null){
            throw new BusinessException("请先进行登录");
        }
        //非空校验
        Integer orderParkId = apply.getOrderParkId();
        Integer rechargeType = apply.getRechargeType();
        BigDecimal sumMoney = apply.getSumMoney();
        String applyRemarks = apply.getApplyRemarks();
        if (orderParkId==null||rechargeType==null||sumMoney==null||StringUtils.isEmpty(applyRemarks)){
            throw new BusinessException("必要字段不能为空");
        }
        apply.setUserId(user.getUserId());
        apply.setApplyType(0);
        apply.setApplyTime(new Date());


        //新增申请
        applyMapper.addApply(apply);

        //修改记录表的退货状态为处理中
        orderParkMapper.updOrderParkBackType(apply.getOrderParkId(),0);


    }

    @Override
    public PageInfo<Apply> getApplyList(ApplyDto applyDto) {
        PageHelper.startPage(applyDto.getPageNum(),applyDto.getPageSize());
        List<Apply> list = applyMapper.getApplyList(applyDto);
        PageInfo<Apply> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Apply getApplyById(Integer applyId) {
        Apply apply = applyMapper.getApplyById(applyId);
        return apply;
    }
}
